package com.magist.manage.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.magist.core.entity.Page;
import com.magist.core.exception.BusinessException;
import com.magist.core.service.SimpleServiceImpl;
import com.magist.manage.web.entity.UserInfo;
import com.magist.manage.web.service.UserAuthService;
import com.magist.manage.web.service.UserInfoService;
import com.magist.manage.web.service.UserLoginService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Service
public class UserInfoServiceImpl extends SimpleServiceImpl<UserInfo> implements UserInfoService {


    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public PageInfo<UserInfo> page(Example example, Page page) {
        PageHelper.startPage(page.getCurrPage(), page.getLimit());
        List<UserInfo> list = this.find(example);
        PageInfo<UserInfo> pageInfo = new PageInfo(list);

        return pageInfo;
    }

    @Override
    public PageInfo<UserInfo> page(UserInfo entity, Page page) {
        PageHelper.startPage(page.getCurrPage(), page.getLimit(), "SHOW_ID DESC");
        List<UserInfo> list = this.find(entity);
        PageInfo<UserInfo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<UserInfo> findByUserName(String userName) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        return this.find(userInfo);
    }

    @Override
    public Integer deleteUser(String userId) {
        if(StringUtils.isEmpty(userId)) {
            throw new BusinessException("删除用户失败");
        }
        Integer result = null;
        result = this.delete(userId);
        // 删除userLogin
        userLoginService.delete(userId);
        // 删除userAuth
        userAuthService.deleteByUserId(userId);
        return result;
    }

    private int delete(String userId) {
        if(StringUtils.isEmpty(userId)) {
            throw new BusinessException("删除用户失败");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        return this.delete(userInfo);
    }

    @Override
    public List<UserInfo> findByCreateById(String userId) {
        UserInfo entity = new UserInfo();
        entity.setCreateById(userId);
        return this.find(entity);
    }

    @Override
    public List<UserInfo> findAuthedUsers(String id) {
        // 查询授权者和授权者创建的数据
        List<UserInfo> allUsers = Lists.newArrayList();
        UserInfo userInfo = new UserInfo();
        userInfo.setAuthById(id);
        List<UserInfo> userInfos = this.find(userInfo);
        allUsers.addAll(userInfos);
        if (!CollectionUtils.isEmpty(userInfos)) {
            userInfos.forEach(userInfo1 -> {
                allUsers.addAll(this.findByCreateById(userInfo1.getId()));
            });
        }
        allUsers.add(this.getById(id));
        // 排除重复的
        List<UserInfo> unique = allUsers.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<UserInfo>(Comparator.comparing(UserInfo::getId))), ArrayList::new)
        );
        return unique;
    }
}